package projectwork.shchuangge.cn.com.newproject.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import projectwork.shchuangge.cn.com.newproject.R;
import projectwork.shchuangge.cn.com.newproject.adapter.MyHomeAdapter;
import projectwork.shchuangge.cn.com.newproject.ui.view.AccessoriesFragment;
import projectwork.shchuangge.cn.com.newproject.ui.view.ActionFragment;
import projectwork.shchuangge.cn.com.newproject.ui.view.ChoiceFragment;
import projectwork.shchuangge.cn.com.newproject.ui.view.LadyFragment;
import projectwork.shchuangge.cn.com.newproject.ui.view.MtmFragment;
import projectwork.shchuangge.cn.com.newproject.ui.view.OtherFragment;
import projectwork.shchuangge.cn.com.newproject.weiget.ReWriteViewPager;

/**
 * Created by 赵航 on 2017/6/17.
 */

public class HomeFragment extends Fragment {
    private ReWriteViewPager mViewPager;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private String[]       arr           = {"精选", "女士", "量身定制", "配饰", "即将上线", "其他"};
    private List<String>   mStrings      = new ArrayList<>();
    private ChoiceFragment      mChoiceFragment;
    private LadyFragment        mLadyFragment;
    private MtmFragment         mMtmFragment;
    private TabLayout           mTabLayout;
    private AccessoriesFragment mAccessor;
    private ActionFragment      mActionFragment;
    private OtherFragment       mOtherFragment;
    private EditText            mEditText;



    private void initData() {

        if (mFragmentList != null || mFragmentList.size() != 0) {
            mFragmentList.clear();
        }

        if (mChoiceFragment == null) {
            mChoiceFragment = new ChoiceFragment();
        }

        if (mLadyFragment == null) {
            mLadyFragment = new LadyFragment();
        }

        if (mMtmFragment == null) {
            mMtmFragment = new MtmFragment();
        }
        if (mAccessor == null) {
            mAccessor = new AccessoriesFragment();
        }
        if (mActionFragment == null) {
            mActionFragment = new ActionFragment();
        }
        if (mOtherFragment == null) {
            mOtherFragment = new OtherFragment();
        }


        mFragmentList.add(mChoiceFragment);
        mFragmentList.add(mLadyFragment);
        mFragmentList.add(mMtmFragment);
        mFragmentList.add(mAccessor);
        mFragmentList.add(mActionFragment);
        mFragmentList.add(mOtherFragment);

        for (String s : arr) {
            mStrings.add(s);
        }
        //初始化适配器
        MyHomeAdapter myHomeAdapter = new MyHomeAdapter(getChildFragmentManager(), mFragmentList, mStrings);
        //设置适配器
        mViewPager.setAdapter(myHomeAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void onPause() {
        super.onPause();
        mStrings.clear();
    }

    @Override
    public void onResume() {
        initData();
        super.onResume();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.home_fragment, null);
        //TODO:键盘监听搜索

        mTabLayout = (TabLayout) view.findViewById(R.id.home_tableLayout);

        mViewPager = (ReWriteViewPager) view.findViewById(R.id.home_viewpager);
        mEditText = (EditText) view.findViewById(R.id.home_serchview_hot_serch);
        //强行隐藏输入法
//        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);


        /**
         * 这里仅仅更改一个正确或者错误就可以让viewpager是否滚动
         *
         * 当设置true的时候是不能滚动的,并且不影响其他空间的点击效果
         */
        mViewPager.setNoScroll(false);
        /**
         * 解决viewpager中fragment人重影问题
         */

        //添加数据
        initData();

        mEditText.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mEditText.setFocusable(true);
                mEditText.setFocusableInTouchMode(true);
                mEditText.requestFocus();
                mEditText.findFocus();
                return false;
            }
        });


        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }
}
